<?php
/**
 * DBGrid class
 *
 * @author fsqijy
 */
require_once   BaseUtil::getModulePath("dbgrid")  .  '/DBGridDraw.php';
require_once   BaseUtil::getModulePath("dbgrid")  .  '/DBGridConstant.php';
class DBGrid {
 
    private $dbGridDraw;
    private $post;

    public function  __construct($view = null) {
        $this->dbGridDraw = new DBGridDraw($view);
    }
    
     public function setFetchTables($tables){
         $this->dbGridDraw->setTables($tables);
     }

     public function addFetchTable($table,$cols,$condition = null,$joinType = DBGridConstant::JOIN_TYPE_INNER){
         $this->dbGridDraw->addTable($table,$cols,$condition,$joinType);
     }

     public function setFetchColumns($columns){
         $this->dbGridDraw->setColumns($columns);
     }

     public function addFetchColumn($name,$label,$action = "",$valueLabel = array(),$sortKey = "",$methodType = DBGridConstant::CALL_SYSTEM_METHOD , $method = ""){
         $this->dbGridDraw->addColumn($name,$label,$action,$valueLabel,$sortKey,$methodType,$method);
     }

     public function addFetchHiddenColumn($name,$valueLabel = array()){
         $this->dbGridDraw->addColumn($name,"","",$valueLabel,"",DBGridConstant::CALL_SYSTEM_METHOD,"",false);
     }

     public function setFilters($filters = array()){
         $this->dbGridDraw->setFilters($filters);
     }

     public function addFilter($type,$name,$label,$operator=DBGridConstant::OPERATOR_LIKE_A,$attr = array()){
         $this->dbGridDraw->addFilter($type,$name,$label,$attr,$operator);
     }

     public function addAdvanceFilter($type,$name,$label,$operator=DBGridConstant::OPERATOR_LIKE_A,$attr = array()){
         $this->dbGridDraw->addAdvanceFilter($type,$name,$label,$attr,$operator);
     }

     public function addOptionsFilter($type,$name,$label,$options=array(),$attr = array(),$operator = DBGridConstant::OPERATOR_EQUAL,$sql = ""){
        $this->dbGridDraw->addOptionsFilter($type,$name,$label,$options,$attr,$operator,$sql);
     }

     public function addAdvanceOptionsFilter($type,$name,$label,$options=array(),$attr = array()){
        $this->dbGridDraw->addAdvanceOptionsFilter($type,$name,$label,$options,$attr);
     }

     public function addValueFilter($name,$value,$operator=DBGridConstant::OPERATOR_EQUAL){
          $this->dbGridDraw->addValueFilter($name,$value,$operator);
     }

     public function addDateFilter($name,$label,$operator=DBGridConstant::OPERATOR_BETWEEN){
          $this->dbGridDraw->addFilter(DBGridConstant::FILTER_TYPE_DATE,$name,$label,array(),$operator);
     }

     public function addAdvanceDateFilter($name,$label,$operator=DBGridConstant::OPERATOR_BETWEEN){
          $this->dbGridDraw->addAdvanceFilter(DBGridConstant::FILTER_TYPE_DATE,$name,$label,array(),$operator);
     }
     
     public function addBetweenFilter($name,$label,$operator=DBGridConstant::OPERATOR_BETWEEN){
          $this->dbGridDraw->addFilter(DBGridConstant::FILTER_TYPE_BETWEEN,$name,$label,array(),$operator);
     }
     
     public function addAdvanceBetweenFilter($name,$label,$operator=DBGridConstant::OPERATOR_BETWEEN){
          $this->dbGridDraw->addAdvanceFilter(DBGridConstant::FILTER_TYPE_BETWEEN,$name,$label,array(),$operator);
     }

     public function addSelectOptionFilter($name,$label,$operator=DBGridConstant::OPERATOR_LIKE_A){
         $this->dbGridDraw->addSelectOptionFilter($name, $label,$operator);
     }

     public function addModuleAction($name,$label,$action,$params=array()){
          $this->dbGridDraw->addModuleAction($name,$label,$action,$params);
     }

     public function addAAModuleAction($controller,$name,$label,$action,$params=array(),$methodType = DBGridConstant::CALL_SYSTEM_METHOD , $method = ""){
        if(AAService::isAllowed($controller, $action)){
            $this->dbGridDraw->addModuleAction($name,$label,$action,$params,$methodType,$method);
        }
     }

     public function addColAction($name,$action,$label="",$image=""){
            $this->dbGridDraw->addColAction($name,$action,$label,$image);
     }

     public function addAAColAction($controller,$action,$name,$label="",$image=""){
            if(AAService::isAllowed($controller, $action)){
                    $this->dbGridDraw->addColAction($name,$action,$label,$image);
            }
     }

     public function setFilterAction($action){
         $this->dbGridDraw->setFilterAction($action);
     }

     public function setPost($post){
         $this->dbGridDraw->setPost($post);
         $this->post = $post;
     }

     public function setItemActions($actions = array()){
         $this->dbGridDraw->setItemActions($actions);
     }

     public function isExport($isExport,$fileName = ""){
            $this->dbGridDraw->setIsExport($isExport);
            $this->dbGridDraw->setExprotFileName($fileName);
     }

//     public function setModuleActions($actions = array()){
//         $this->_moduleActions = $actions;
//     }

     public function setModule($module){
         $this->dbGridDraw->setModule($module);
     }

     public function setSortOrder($col,$order = 'DESC'){
         $this->dbGridDraw->setSortOrder($col, $order);
     }

     public function setPageTitle($pageTitle,$pageTitleLink = ""){
         $this->dbGridDraw->setPageTitle($pageTitle,$pageTitleLink);
     }
     
     public function setReturnData($returnData){
        $this->dbGridDraw->setReturnData($returnData);
     }

     public function setPrimaryKey($key){
         $this->dbGridDraw->setPrimaryKey($key);
     }

     public function setCallBackMethod($method){
         $this->dbGridDraw->setCallBackMethod($method);
     }

     public function setFilterColNum($filterColNum){
         $this->dbGridDraw->setFilterColNum($filterColNum);
     }

     public function render(){
         $action = "";
         if(isset($this->post) && isset($this->post['dbgridAction'])){
             $action = trim($this->post['dbgridAction']);
         }
         $html = "";
         if($action == 'export'){
             $this->dbGridDraw->exportData();
         }else{
             $html = $this->dbGridDraw->render();
         }
         return $html;
     }

     public function renderByAjax(){
         $html = "";
         $html = $this->dbGridDraw->renderByAjax();
         return $html;
     }

    /**
     * for ajax
     */
     public function renderAjaxDbGrid(){
         $action = "";
         if(isset($this->post) && isset($this->post['dbgridAction'])){
             $action = trim($this->post['dbgridAction']);
         }
         $html = "";
         if($action == 'export'){
             $this->dbGridDraw->exportData();
         }else{
             $html = $this->dbGridDraw->renderAjaxDbGrid();
         }
         return $html;
     }
}
